<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Validate user input for getting captcha
 * @access public
 * @return boolean Returns true if valid, otherwise returns false
 */
function valid_captcha()
{
    $ci = &get_instance();
    
    $ci->form_validation
        ->set_rules('length', 'length', 'required|numeric|max_length[50]|xss_clean')
        ->set_rules('path', 'path', 'required|max_length[255]|xss_clean')
        ->set_rules('expiration', 'expiration', 'required|numeric|max_length[10]|xss_clean')
        ->set_rules('width', 'width', 'required|numeric|max_length[5]|xss_clean')
        ->set_rules('height', 'height', 'required|numeric|max_length[5]|xss_clean');

    if ($ci->form_validation->run() === FALSE)
    {
        return FALSE;
    } else
    {
        return TRUE;
    }
}

/*
 * Validate user input for retrieving data
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise false
 */
function valid_retrieve_data()
{
    $ci = &get_instance();
    $ci->form_validation->set_rules('db-name', 'database name', 'trim|required|max_length[255]|xss_clean')
        ->set_rules('db-attr-value', 'database attribute value', 'trim|required|max_length[1000]|xss_clean')
        ->set_rules('db-attr-text', 'database attribute text', 'trim|required|max_length[1000]|xss_clean')
        ->set_rules('db-attr-image', 'database attribute image', 'trim|required|max_length[1000]|xss_clean');
    
    if ($ci->form_validation->run() === FALSE)
    {
        return FALSE;
    } else
    {
        return TRUE;
    }
}

/**
 * Initialize Messages that will be shown on the user screen.
 * @access public 
 */
function init_messages()
{
    $ci = &get_instance();
    $ci->messages['edit_success'] = 'Edit Successful';
    $ci->messages['edit_failed'] = 'Edit Failed';
    $ci->messages['server_error'] = 'Request Failed';
}

/*
 * Method for validating form inputs from the user form.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise redirects to the info form.
 */
function valid()
{
    $ci = &get_instance();
    $ci->form_validation->set_rules('key', 'key', 'trim|required|max_length[255]|xss_clean');
    $ci->form_validation->set_rules('content', 'content', 'trim|required|max_length[1000]|xss_clean');
    if ($ci->form_validation->run() == FALSE)
        display('edit_failed');
    else
        return true;
}

/**
 * Displays the generator form.
 * @acces public
 * @param string $message The message type that will be shown on the generator.
 */
function view_form($message = NULL)
{
    $ci = &get_instance();
    
    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Generator');

    
    
    $data['message_type'] = $message;
    $data['message'] = $message ? $ci->messages[$message] : $message;
    
    // set active navigation
    $data['navigation'] = 'generator';
    
    // set stylesheet, script, template and view
    $ci->template
            ->title('Generator')
            ->set_layout('cms')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('notifications', 'layouts/notifications')
            ->set_partial('notifications', 'layouts/modal')
            ->build('view_form', $data);
}

/*
 * Method for validating form inputs from the user form.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise redirects to the info form.
 */
function valid_from()
{
    $ci = &get_instance();
    $ci->form_validation->set_rules('email', 'email', 'trim|required|max_length[255]|valid_email|xss_clean');
    $ci->form_validation->set_rules('cc', 'cc', 'trim|max_length[255]|valid_emails|xss_clean');
    $ci->form_validation->set_rules('bcc', 'bcc', 'trim|max_length[255]|valid_emails|xss_clean');
    $ci->form_validation->set_rules('response', 'response message', 'trim|required|max_length[10000]|xss_clean');
    
    if ($ci->form_validation->run() == FALSE)
    {
        view_form('edit_failed');
    }
    else
    {
        return TRUE;
    }
}

/**
 * Checks if another table is to be joined
 * @param array $fields An array of field information
 * @return boolean Returns true if another table is to be joined, otherwise false
 */
function has_join_tables($fields)
{
    $has_join_tables = FALSE;
    foreach($fields as $field)
    {
        if($field['type'] === 'radio' OR $field['type'] === 'dropdown' )
        {
            if($field['single_data'] === 'database')
            {
                $has_join_tables = TRUE;
                break;
            }
        }
    }
    return $has_join_tables;
}

/**
 * Check if using file upload
 * @param array $fields Array of field data
 * @return boolean Returns true if file input is present, otherwise false
 */
function has_file($fields)
{
    $has_file = FALSE;
    foreach($fields as $field)
    {
        if($field['type'] === 'upload_file' OR $field['type'] === 'upload_image' OR $field['type'] === 'upload_url' OR $field['type'] === 'upload_multiple' )
        {
            $has_file = TRUE;
            break;
        }
    }
    return $has_file;
}

/* End of file generator_helper.php */
/* Location: ./application/modules/cms-generator/helpers/generator_helper.php */